import Taro from '@tarojs/taro'
import { HTTP_STATUS } from '../utils/const'
import { base_url } from './config'

const token = '95c1eb1e984147408ea4f992cc7b26c3'

export default {
  baseOptions(params, method) {
    let { url, data } = params
    // let token = getApp().globalData.token
    // if (!token) login()

    let pData = {};

    pData.appId = 'KC876D98B30UA40CJSNHI689FB33SDW';
    pData.digest = 'dda484557c12febe48139d8f44051c13';
    pData.platform = 'android';
    pData.token = token;
    pData.loginChannel = 'c'

    console.log('params', params)
    let contentType = 'application/x-www-form-urlencoded'
    contentType = params.contentType || contentType
    const option = {
      url: base_url + url,
      data: pData,
      method: 'GET',
      header: { 'content-type': contentType , 'token' : token},
      success(res) {
        // if (res.statusCode === HTTP_STATUS.NOT_FOUND) {
        //   return '请求资源不存在'
        // } else if (res.statusCode === HTTP_STATUS.BAD_GATEWAY) {
        //   return '服务端出现了问题'
        // } else if (res.statusCode === HTTP_STATUS.FORBIDDEN) {
        //   return '没有权限访问'
        // } else if (res.statusCode === HTTP_STATUS.SUCCESS) {
        //   console.log('success-------' + JSON.stringify(res.data))
        //   return res.  data
        // }
        console.log('res-------------'+  JSON.stringify(res))

        if (res.statusCode && res.data){
          if (res.statusCode === HTTP_STATUS.SUCCESS){
            console.log('success-------' + JSON.stringify(res.data))
            return res.data
          } else {

          }
        }
      },
      error(e) {
        console.log('error-------' + JSON.stringify(e))
      }
    }
    return Taro.request(option)
  },
  get(url, data = '') {
    let option = { url, data }
    return this.baseOptions(option)
  },
  post: function (url, data, contentType) {
    let params = { url, data, contentType }
    return this.baseOptions(params, 'POST')
  }
}
